const {verifyToken} = require('../utils/tokenUtils');
const {DATAERROR} = require('../utils/constants');

module.exports = (req,res,next)=>{

   //  预检请求判断
   if(req.method === 'OPTIONS'){
      next();
      return;
   }

   let token = req.headers.token;
   try{
    let result = verifyToken(token);
    // console.log(res); //{ username: 'admin', iat: 1683772804, exp: 1683859204 }
    req.userInfo = result;
    next()
   }catch(err){
     res.json({
         code:DATAERROR,
         message:err.message
     })
   }
}